<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld"%>
<!DOCTYPE html>
<html>
	<head>
		<title>菜单添加</title>
		<!-- 引入公用Js与Css -->
  		<%@include file="/view/common/resource.jsp" %>
	</head>
	<body>
		<div id="wrap">
			
			<div class="clearfix mt10 mr10 ml10">
				<form id="menuForm" class="form-horizontal" action="<%=basePath%>/sys/menu/addSave" method="post">
					<dl class="ml10">
					
						<dd class="clearfix pt10">
							<label class="w80 mr10 pull-left text-right line-height30">菜单名称:</label>
							<div class="w300 input-group input-group-sm" >
					            <input type="text" class="form-control" id="menuName" name="menuName" placeholder="请填写菜单的名称(必填项)">
							</div>
						</dd>
						
						<dd class="clearfix pt10">
							<label class="w80 mr10 pull-left text-right line-height30">菜单级别:</label>
							<div class="w300 input-group input-group-sm" >
								<select id="menuGrade" name="menuGrade" class="form-control">
									<option value="">请选择</option>
									<c:forEach var="item" items="${fns:getDictList('menu_grade')}">
										<option value="${item.dictValue }">${item.dictName }</option>
									</c:forEach>
								</select>
					        </div>
						</dd>
						
						<div id="parentMenuDiv" class="hide">
							<dd class="clearfix pt10">
								<label class="w80 mr10 pull-left text-right line-height30">上级菜单:</label>
								<div class="w300 input-group input-group-sm" >
									<select id="parentMenuId" name="parentMenu.id" class="form-control">
										<option value="">请选择</option>
										<c:forEach var="item" items="${dataList}">
											<option value="${item.id }">${item.menuName }</option>
										</c:forEach>
									</select>
						        </div>
							</dd>
						</div>
						
						<dd class="clearfix pt10">
							<label class="w80 mr10 pull-left text-right line-height30">菜单图标:</label>
							<div class="w300 input-group input-group-sm" >
					            <input type="text" class="form-control" id="menuImg" name="menuImg" placeholder="请填写font-awesome4.7图标">
							</div>
						</dd>
						
						<dd class="clearfix pt10">
							<label class="w80 mr10 pull-left text-right line-height30">菜单链接:</label>
							<div class="w300 input-group input-group-sm" >
					            <input type="text" class="form-control" id="menuUrl" name="menuUrl" placeholder="一级菜单不必填写">
							</div>
						</dd>
					
						<dd class="clearfix pt30 ml110">
							<p class="radius3 bg_blue text-center w200 line-height30 mt10">
								<a href="javascript:" onclick="addSave();" class="color_white show">确认添加</a>
							</p>
						</dd>
					</dl>
				</form>
			</div>
		</div>
		<script type="text/javascript">
	    var validate = null;
	    $(document).ready(function(){
	    	validate = $('#menuForm').validate({
	    		onkeyup:false,
				onfocusout: function(element){
					if (element.value != null && element.value != "") {
						$(element).valid();
					}
			    },
				rules:{
					menuName:{
						required:true
					},
					menuGrade:{
						required:true
					}
				},
				messages:{
					menuName:{
						required:"请输入菜单名称"
					},
					menuGrade:{
						required:"请选择菜单级别"
					}
				},
				errorPlacement: function(error, element) {
					layer.tips(error.text(), '#'+element.attr("name"), {
					  tips: [2, '#78BA32']
					});
				}
			});
	    	
	    	// 菜单级别变动
	    	$("#menuGrade").change(function() {
	    		var menuGrade = $(this).val();
	    		if (menuGrade == 2) {
	    			$("#parentMenuDiv").removeClass("hide");
	    			$("#parentMenuDiv").show();
	    		} else {
	    			$("#parentMenuDiv").hide();
	    			$("#parentMenuId").val("");
	    		}
	    	});
	    });
	    
    	/* 注册数据保存 */
	    function addSave() {
    		
    		// 验证数据
	    	if (!validate.form()) {
	    		return false;
	    	}
	    	
    	    var actionurl=$('#menuForm').attr('action');//提交路径
    	    
    	    $("#menuForm").ajaxSubmit({
    	    	type:"post",
    	    	url:actionurl,
    	    	dataType: "json",
    	    	success:function(data){
        	    	if (data.success) {
    	   	        	// 成功后跳转首页
    	   	            layer.msg("菜单添加成功", {icon: 6, time:1000}, function() {
    	   	            	var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    	   	            	parent.layer.close(index);
    	   	            });
    	   	        } else {
    	   	      		// 失败提醒
    	   	        	layer.msg(data.msg, {icon: 5});
    	   	        }
       	    	}
    	    }); 
	    }
    </script>
	</body>
</html>
